package com.usian.controller;

import com.usian.pojos.Post;
import com.usian.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping("/toadd")
    public String toadd(){

        return "addPost";
    }

    @GetMapping("/toupdate")
    public String toupdate(Integer id,Model model){

        Post post = postService.getById(id);

        model.addAttribute("post",post);

        return "updatePost";
    }

    @GetMapping("/findPost")
    public String findPost(Model model){

        List<Post> post = postService.findPost();

        model.addAttribute("post",post);

        return "post";
    }

    @PostMapping("/addPost")
    public String addPost( Post post){

        postService.addPost(post);

        return "redirect:/post/findPost";
    }

    @PostMapping("/updatePost")
    public String updatePost( Post post){

        postService.updatePost(post);

        return "redirect:/post/findPost";
    }

    @GetMapping("/delPost")
    public String delPost(Integer id){

        postService.delPost(id);

        return "redirect:/post/findPost";
    }
}
